package com.itserver.controller.sysController;

import com.itserver.ano.UpdateUser;
import com.itserver.pojo.PageResult;
import com.itserver.pojo.Result;
import com.itserver.pojo.sys.QueryUser;
import com.itserver.pojo.sys.User;
import com.itserver.service.sys.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("sys/user")
public class UserController {
    @Autowired
    private UserService userService;

    //@Log
    //用户列表
    @GetMapping
    public Result list(QueryUser queryUser) {
        log.info("查询用户列表");
        PageResult<User> users = userService.list(queryUser);
        return Result.success(users);
    }

    // 用户新增
    @PostMapping
    public Result add(@RequestBody User user) {
        log.info("用户新增");
        // 设置用户类型 0注册 1新增
        user.setUserType(1);
        userService.add(user);
        return Result.success();
    }

    // 修改用户
    @PutMapping
    public Result update(@RequestBody User user){
        userService.update(user);
        return Result.success();
    }

    @PutMapping("/info")
    @UpdateUser
    public Result myUpdate(@RequestBody User user){
        user.setRoles(null);
        userService.update(user);
        return Result.success();
    }
    // 删除用户 逻辑删除
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        userService.delete(ids);
        return Result.success();
    }

    // 获取用户详情
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }
}
